package com.sun.dailyprj;

import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;

import androidx.annotation.Nullable;

/*
 * =====================================================================================
 * Summary:
 *
 * File: DailyService.java
 * Author: Yanpeng.Sun
 * Create: 2019/6/3 10:04
 * =====================================================================================
 */
public class DailyService extends Service{

    private WindowManager mWindowManager;
    private WindowManager.LayoutParams mParams;
    private View mView;
    private Button mBtnSwitch, mBtnLauncher;
    private Context mContext;

    public static void start(Context context) {
        context.startService(new Intent(context, DailyService.class).setPackage(context.getPackageName()));
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mContext = this;
        mWindowManager = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
//        initToastAlert();
        return START_STICKY;
    }

    private void initToastAlert() {
        mParams = new WindowManager.LayoutParams();
        mParams.gravity = Gravity.RIGHT | Gravity.TOP;
        mParams.height = WindowManager.LayoutParams.MATCH_PARENT;
        mParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        mParams.x = 0;
        mParams.y = 400;
        mParams.width = 120;
        mParams.height = 240;
        mParams.format = PixelFormat.TRANSLUCENT;
        mParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
        mParams.windowAnimations = android.R.style.Animation_Toast;
        mParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;

        LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if(null != inflater)
            mView = inflater.inflate(R.layout.alert_upgrade, null);
        mWindowManager.addView(mView, mParams);
        initViewId();
    }

    private void initViewId() {
        mBtnSwitch = mView.findViewById(R.id.btn_usb_switch);
        mBtnSwitch.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                ComponentName cn = new ComponentName("com.haoke.mx6usbswitch", "com.haoke.mx6usbswitch.MainActivity");
                intent.setComponent(cn);
                mContext.startActivity(intent);
            }
        });
        mBtnLauncher = mView.findViewById(R.id.btn_launcher);
        mBtnLauncher.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                ComponentName cn = new ComponentName("com.android.launcher", "com.android.launcher2.Launcher");
                intent.setComponent(cn);
                mContext.startActivity(intent);
            }
        });
    }
}
